package net.mingsoft.document.aspect;

import net.mingsoft.document.aspect.annotation.FileLog;
import net.mingsoft.document.biz.FileLogBiz;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;

/**
 * @author gengfa
 * @date 22/01/28
 */
@Aspect
@Component
public class FileLogAspect {

    @Resource
    FileLogBiz fileLogBiz;

    @Pointcut("@annotation(net.mingsoft.document.aspect.annotation.FileLog)")
    public void pointCut() {}

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        saveLog(joinPoint);
        return joinPoint.proceed();
    }

    public void saveLog(ProceedingJoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        FileLog fileLog = method.getAnnotation(FileLog.class);

        fileLogBiz.record(fileLog.value(), fileLog.operateType());
    }
}
